﻿@inherits AdminCompontentBase

<SSheetDialog EnableDomReload @bind-Value=_visible Title="@T("Ldap Configure")">
    <MCard class="pt-9 d-flex flex-column full-height" Style="margin-left:228px;margin-right:228px;">
        <MCardText Class="full-height">
            <MForm Model=_ldapDetailDto EnableValidation EnableI18n @ref="_form">
                <MRow>
                    <MCol Md=12>
                        <STextField Label="@T("LDAP Root User")" @bind-Value="_ldapDetailDto.RootUserDn"></STextField>
                    </MCol>
                    <MCol Md=12>
                        <STextField Label="@T("LDAP Root User Pwd")" @bind-Value="_ldapDetailDto.RootUserPassword"></STextField>
                    </MCol>
                    <MCol Md=12 Class="d-flex align-center">
                        <STextField Label="@T("Server Address")" Style="width:calc(100vh - 320px);" @bind-Value="_ldapDetailDto.ServerAddress"></STextField>
                        <STextField Style="max-width:140px;" Class="mx-4" Clearable=false Label="@T("Server Port")" @bind-Value="_ldapDetailDto.ServerPort"></STextField>
                        <MCheckbox @bind-Value="_ldapDetailDto.IsLdaps" Class="mt-0" Style="width:140px;" Label="@T("Is LDAPS")" TValue="bool"></MCheckbox>
                    </MCol>
                    <MCol Md=12>
                        <STextField Label="@T("BaseDn")" @bind-Value="_ldapDetailDto.BaseDn"></STextField>
                    </MCol>
                    <MCol Md=12>
                        <STextField Label="@T("User Search BaseDn")" @bind-Value="_ldapDetailDto.UserSearchBaseDn"></STextField>
                    </MCol>
                    <MCol Md=12>
                        <STextField Label="@T("Group Search BaseDn")" @bind-Value="_ldapDetailDto.GroupSearchBaseDn"></STextField>
                    </MCol>
                </MRow>
            </MForm>
        </MCardText>
        <MCardActions class="d-flex align-center mb-12 px-6">
            <MSpacer />
            <SButton class="rounded-pill mr-6" MinWidth="100" OnClick="ConnectTest">@T("Test")</SButton>
            <SButton class="rounded-pill primary" MinWidth="100" Loading=_loading OnClick="LdapSubmit">@T("Submit")</SButton>
        </MCardActions>
    </MCard>
</SSheetDialog>

@code {
    MForm _form = null!;
    LdapDetailDto _ldapDetailDto = new();
    bool _visible;
    bool _loading;

    ThirdPartyIdpService ThirdPartyIdpService => AuthCaller.ThirdPartyIdpService;

    [Parameter]
    public Func<Task>? OnSubmitted { get; set; }

    protected override void OnInitialized()
    {
        PageName = "ThirdPartyIdpBlock";
        base.OnInitialized();
    }

    public async Task OpenAsync()
    {
        _ldapDetailDto = await ThirdPartyIdpService.GetLdapDetailAsync();
        _visible = true;
        StateHasChanged();
    }

    private async Task ConnectTest()
    {
        if (!_form.Validate())
        {
            return;
        }

        try
        {
            //todo remove try catch
            await ThirdPartyIdpService.LdapConnectTestAsync(_ldapDetailDto);
            OpenSuccessMessage(T("Connect Success"));
        }
        catch (UserFriendlyException e)
        {
            OpenErrorMessage(e.Message);
        }
        catch
        {
            OpenErrorMessage(T("Connect Error"));
        }
    }

    private async Task LdapSubmit()
    {
        if (!_form.Validate())
        {
            return;
        }
        _loading = true;
        try
        {
            await ThirdPartyIdpService.LdapUpsertAsync(_ldapDetailDto);
            OpenSuccessMessage(T("LDAPAccountSynchronizPrompt"));
            _visible = false;
            if (OnSubmitted != null)
            {
                await OnSubmitted.Invoke();
            }
        }
        catch (Exception e)
        {
            OpenErrorMessage(e.Message);
        }
        finally
        {
            _loading = false;
        }
    }
}
